<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>2、安装并部署hexo博客 | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="Hexo博客搭建" />
  
  
  
  
  <meta name="description" content="Hexo博客搭建">
<meta name="keywords" content="Hexo博客搭建">
<meta property="og:type" content="article">
<meta property="og:title" content="2、安装并部署Hexo博客">
<meta property="og:url" content="http://www.hais2.com/2016/03/05/Hexo-Getting-Started-2/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="Hexo博客搭建">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1map7rgv4j209y0e175a.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1mbhjwda1j20vy0h6q45.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1memdmu7fj212n0m1wn6.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1mc2nd05dj20ul08wac4.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f5i3vr82tpj20qs0h5q3y.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/c55a7aeejw1f1mcjap8y1j20d202et8s.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1mcvvqn27j20s003fglp.jpg">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="2、安装并部署Hexo博客">
<meta name="twitter:description" content="Hexo博客搭建">
<meta name="twitter:image" content="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1map7rgv4j209y0e175a.jpg">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Hexo-Getting-Started-2" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      2、安装并部署Hexo博客
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2016/03/05/Hexo-Getting-Started-2/" class="article-date">
	  <time datetime="2016-03-05T12:46:11.000Z" itemprop="datePublished">March 5, 2016</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <p>　　下面的搭建过程是在Windows系统上进行的，Linux系统和OSX系统可以看<a href="https://hexo.io/zh-cn/docs/index.html" target="_blank" rel="noopener">官方文档</a>。</p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>　　因为Hexo是基于nodejs编写的，而且使用Git进行管理，所以需要安装这两个工具。<br><strong>安装Git工具</strong><br>　　直接百度Git的Windows安装包或者去<a href="http://git-scm.com/download/" target="_blank" rel="noopener">官网下载</a>安装，安装过程中无需特殊设置，一路next即可。</p>
<p><strong>安装NodeJs</strong><br>　　nodejs也可以直接去<a href="http://nodejs.cn/" target="_blank" rel="noopener">中文网</a>或<a href="http://nodejs.org/" target="_blank" rel="noopener">英文官网</a>下载。nodejs有两个版本：LTS(长期支持版)、Stable(稳定版)。建议大家下载LTS长期支持版。<br><a id="more"></a><br><strong>安装Hexo</strong><br>　　将Hexo安装到电脑中，这样就可以在全局使用<code>hexo</code>命令进行操作了。<br>　　在任意地方鼠标右击，选择“Git Bash Here”。(一般Git工具安装成功的话右键菜单都会有这个选项，如果没有的话请查看电脑已安装的软件中有没有Git，如果没有重装或者重启电脑试试看。)<br><img src="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1map7rgv4j209y0e175a.jpg" alt=""><br>　　在命令行窗口中输入命令然后回车：<br><figure class="highlight avrasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g hexo-<span class="keyword">cli</span></span><br></pre></td></tr></table></figure></p>
<h2 id="创建并初始化Hexo博客"><a href="#创建并初始化Hexo博客" class="headerlink" title="创建并初始化Hexo博客"></a>创建并初始化Hexo博客</h2><p><strong>创建Hexo博客文件夹</strong><br>　　在任何合适的文件夹下面(比如我的创建在E:\Is2Blog)，在该文件夹中鼠标右击，依旧选择“Git Bash Here”打开命令行窗口。<br>　　输入初始化命令：<br><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">hexo init</span></span><br></pre></td></tr></table></figure></p>
<p>　　完成后应该可以看到多了几个文件夹。<img src="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1mbhjwda1j20vy0h6q45.jpg" alt=""></p>
<p><strong>安装部署工具</strong><br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="keyword">install</span> hexo-deployer-git <span class="comment">--save</span></span><br></pre></td></tr></table></figure></p>
<p>　　这一步是安装部署博客的组件，在后面部署博客时遇到的大多数错误都是由于没有安装这个组件，所以避免后面出错，我将这个组件的安装操作提前到了最开始。</p>
<p><strong>本地安装完成</strong><br>　　到目前为止我们已经在本地创建好了Hexo博客，并且已经可以在本地访问了：<br>　　在刚刚创建Hexo的文件夹下使用“Git Bash Here”打开命令行窗口，分别输入下面的两行命令：<br><figure class="highlight verilog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hexo <span class="keyword">generate</span></span><br><span class="line">hexo server</span><br></pre></td></tr></table></figure></p>
<p>　　这两条命令的作用分别是生成博客静态文件、启动预览服务。<br>　　这时可以看到窗口中提示我们访问<code>http://localhost:4000</code>了，现在复制这个地址在浏览器中打开试试。<br><img src="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1memdmu7fj212n0m1wn6.jpg" alt=""></p>
<h2 id="将本地博客部署到github上"><a href="#将本地博客部署到github上" class="headerlink" title="将本地博客部署到github上"></a>将本地博客部署到github上</h2><p>　　现在这个博客还只能在本地访问，因为我们还没有部署到服务器上面。<br>　　Hexo可以部署至云服务器或其他类型的服务器，这里我们只是简单的技术博客，可以使用github免费提供的pages服务，托管我们的博客。</p>
<p><strong>注册github</strong><br>　　可以登录<a href="https://github.com/" target="_blank" rel="noopener">https://github.com/</a>注册一个账号。</p>
<p><strong>创建pages项目</strong><br>　　注册成功后，登录到主页，然后创建一个用来托管博客的pages页面项目：<br><img src="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1mc2nd05dj20ul08wac4.jpg" alt=""><br><img src="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f5i3vr82tpj20qs0h5q3y.jpg" alt=""></p>
<p>　　创建完成之后，github项目库应该是这种形式的：<img src="http://ww3.sinaimg.cn/mw690/c55a7aeejw1f1mcjap8y1j20d202et8s.jpg" alt=""></p>
<p><strong>将本地博客部署到github</strong><br>　　现在我们可以将本地的Hexo博客部署到刚才创建的库中，以便能够通过网络访问我们的博客。<br>　　部署的操作，之后会使用hexo命令行进行部署，现在我们先配置提交方式和地址：<br>　　在我们创建的博客文件夹下找到<code>_config.yml</code>文件，使用编辑器打开，在文件最后输入下面内容：<br><figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">deploy</span>:</span><br><span class="line">  <span class="attribute">type</span>: git</span><br><span class="line">  <span class="attribute">repository</span>: <span class="attribute">https</span>:<span class="comment">//github.com/username/username.github.io.git</span></span><br><span class="line">  <span class="attribute">branch</span>: master</span><br></pre></td></tr></table></figure></p>
<p>　　在添加上面的配置时请注意：<br>　　1. 每一个冒号(:)之后应该有一个半角空格，否则无法拿到后面的值。<br>　　2. <code>repository</code>的值是你的项目的地址，可以在你的项目中找到，比如：<img src="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f1mcvvqn27j20s003fglp.jpg" alt=""></p>
<p>　　上面的配置完成之后，现在可以将博客文件提交至github库中了，在“Git Bash Here”命令行窗口中分别输入下面的命令：<br><figure class="highlight verilog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hexo <span class="keyword">generate</span></span><br><span class="line">hexo deploy</span><br></pre></td></tr></table></figure></p>
<p>　　上面的命令可以使用简写形式缩短至一行：<code>hexo d -g</code> ，也就是生成并部署。</p>
<p>　　PS：在部署提交的时候，可能会提示你输入github的账号和密码，输入即可，在之后会介绍使用SSH密钥的方式提交，免去每次提交都要输入账号密码的麻烦。</p>
<p><strong>通过网址访问</strong><br>　　现在博客已经部署到github中了，由github托管，已经可以通过链接的形式访问了，可以在浏览器中输入你创建的库的名称，比如我的是：haijiandong.github.io<br>　　将你的地址输入到浏览器试试，如果碰到404的情况，有两种解决办法：<br>　　1. 等待一会儿再访问，可能是网络有延迟。<br>　　2. 打开你注册时填写的邮箱，看看是否有github发过来的邮件(注意垃圾箱)，如果有的话打开看看，很多时候都是没有注意到github的邮件造成404错误。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Hexo/">Hexo</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Hexo博客搭建/">Hexo博客搭建</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/03/05/Hexo-Getting-Started-3/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          3、Hexo博客配置和主题
        
      </div>
    </a>
  
  
    <a href="/2016/03/05/Hexo-Getting-Started-1/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">1、Hexo博客简介</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装"><span class="nav-number">1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建并初始化Hexo博客"><span class="nav-number">2.</span> <span class="nav-text">创建并初始化Hexo博客</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#将本地博客部署到github上"><span class="nav-number">3.</span> <span class="nav-text">将本地博客部署到github上</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
